function checkdate(date) {
    if (date > 9) {
        return date;
    } else {
        return '0' + date;
    }
}

$(function () {

    //首次打开页面时获取默认数据
    energyAnalysis($("#customerinfoid").val(), $("#chartType").val(), $("#startDate").val(), $("#endDate").val());

    //默认时间
    var today = new Date();
    var thismonth = today.getMonth() + 1;
    $("#endDate").val(today.getFullYear() + '-' + checkdate(thismonth));
    var sevenago = today.getMonth() - 7;
    today.setMonth(sevenago);
    var sevenagomonth = today.getMonth() + 1;
    $("#startDate").val(today.getFullYear() + '-' + checkdate(sevenagomonth));

    //选择一个客户
    $("#companyName").dblclick(function () {
        $.baseMultiCustomerApi({
            getBaseValue: function (customers) {
                $("#customerinfoid").val((_.pluck(customers, "id")).join(","));
                $("#companyCode").val((_.pluck(customers, "companycode")).join(","));
                $("#companyName").val((_.pluck(customers, "customername")).join(","));
            },
        });
    });

    //执行查询操作
    $("#searchEnergy").click(function () {
        energyAnalysis($("#customerinfoid").val(), $("#chartType").val(), $("#startDate").val(), $("#endDate").val());
    });

    function energyAnalysis(customerinfoid, chartType, startDate, endDate) {
        var param = {
            "customerinfoid": customerinfoid,
            "startDate": startDate,
            "endDate": endDate
        };
        $.ajax({
            contentType: "application/json",
            url: "getUnitEnergyByDate.shtml",
            data: param,
            async: false,
            dataType: "json",
            success: function (data) {
                //获取所有日期
                var dates = _.uniq(_.sortBy(_.pluck(data, 'statisticsdate')));
                var group = _.groupBy(data, 'customername');
                var keys = _.keys(group);
                var series = [];
                for (var i = 0; i < keys.length; i++) {
                    var serie = {};
                    serie.name = keys[i];
                    var property = _.property(keys[i])(group);
                    //考虑到有的公司某些月份时数据为空的情况，因此需要补充数据
                    if (property.length < dates.length) {
                        for (var j = 0; j < dates.length; j++) {
                            if (!_.contains(_.pluck(property, 'statisticsdate'), dates[j])) {
                                var obj = {};
                                obj.statisticsdate = dates[j];
                                obj.unitperson = 0;
                                obj.unitsquare = 0;
                                obj.unitsale = 0;
                                property[property.length] = obj;
                            }
                        }
                    }
                    serie.data = _.pluck(_.sortBy(property, 'statisticsdate'), chartType);
                    series[i] = serie;
                }
                linear('container', '综合经营分析表', dates[0] + '~' + dates[dates.length - 1], dates, $("#chartType option:selected").text(), '', series, true);
            }
        });
    }
});
